/*
 * 羊只信息修改
 * @Author: ljb 
 * @Date: 2017-11-27 15:24:41 
 * @Last Modified by: ljb
 * @Last Modified time: 2018-01-30 11:04:02
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .ivu-form-item-content {
        img {
            width: 150px;
        }
    }

</style>
<template>
<div class="v-admin-Product-edit">
    <Form ref="formValidate" :model="formValidate" :label-width="80">

        <Form-item label="视频类别" prop="video_type_id">
            <Select v-model="formValidate.video_type_id" clearable filterable>
                <Option v-for="( value , key ) in videoTypes" :value="key + ''" :key="key">{{ value }}</Option>
            </Select>
        </Form-item>

        <Form-item label="视频名称" prop="name">
            <Input type="text" v-model="formValidate.name" placeholder="请输入名称"></Input>
        </Form-item>

        <Form-item label="优先级" prop="sort">
            <InputNumber :max="100" :min="1" v-model="formValidate.sort" placeholder="请输入视频优先级"></InputNumber>
        </Form-item>

        <Form-item label="封面图" prop="preview_img">
            <Upload :before-upload="file=>fileChange(file,'preview_img','image_info')"
                    :on-remove="file=>fileChange(file,'preview_img','image_info')"
                    :multiple="false"
                    accept="image/jpg, image/jpeg, image/png"
                    action="//fake_upload_url/">
                <Button type="ghost" icon="ios-cloud-upload-outline">上传图片</Button>
                <div class="license-tip" slot="tip" v-if="image_info.name">{{ image_info.name }}</div>
            </Upload>
            <img v-if="image_info.url" :src="image_info.url" alt="封面图">
        </Form-item>

        <Form-item label="视频路径" prop="url">
            <video :src="formValidate.url" width="200" height="150" controls="controls"></video>
        </Form-item> 
        
        <Form-item>
            <Button type="primary" @click="updateProduct">更新</Button>
        </Form-item>

    </Form>
</div>
</template>

<script>

import API_LIST from 'src/API_LIST';

export default {
    name: 'video-edit',
    props: ['videoDetail','videoTypes'],
    data() {	
        return {

            formValidate : {

                name : '',

                video_type_id : '',

                url : '',

                preview_img: '',

                sort: 1
            },                  //更新视频时的表单

            // 上传的图片信息
            image_info : {
                name: '',
                url: ''         //这个地方可以 base64 编码的图片，然后给 modal 框去显示图片
            }

        }
    },
    watch: {
        'videoDetail' : function(){

            if( this.videoDetail ) {

                this.formValidate.name = this.videoDetail.name;
                this.formValidate.video_type_id = String(this.videoDetail.video_type_id);
                this.formValidate.preview_img = this.videoDetail.preview_img;
                this.formValidate.sort = this.videoDetail.sort;
                this.formValidate.url = this.videoDetail.url;

                this.image_info.url = this.videoDetail.preview_img;

            }

        }  
    },
    methods: {

        /**
         * file类型input改变
         * @param  {Object} file {选择的文件对象}
         * @return {Boolean} {false则会停止上传}
         */
        fileChange(file,param,info){

            this[info].name = file.name;

            this[info].url = '';

            this.formValidate[param] = file;

            return false;

        },

        /**
         * 重置合作伙伴信息
         * @return {undefined}
         */
        resetProductInfo(){

            this.image_info.name = '';

            this.image_info.url = '';

        },

        /**
         * 更新合作伙伴信息
         * @return {undefined}
         */
        updateProduct(){
            
            this.resetProductInfo();

            this.$emit('update',this.videoDetail.id,this.formDataCreate());

        },

        /**
         * 手动拼接formdata数据
         * @return {Object}
         */
        formDataCreate(){

            const new_object = {

                _method : 'PUT',

                name : this.formValidate.name,

                video_type_id : this.formValidate.video_type_id,

                preview_img : this.formValidate.preview_img,

                url : this.formValidate.url,

                sort: this.formValidate.sort

            };

            let form_data = new FormData();

            Object.keys(new_object).forEach((key)=>{

                form_data.append( key , new_object[key] );
        
            });

            return form_data;

        }
        
    }
}
</script>